package jsu.yym.tiktok.controller;

import jsu.yym.tiktok.pojo.ResponseResult;
import jsu.yym.tiktok.pojo.User;
import jsu.yym.tiktok.pojo.Video;
import jsu.yym.tiktok.service.SearchService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
public class searchController {
    @Resource
    private SearchService searchService;

//    用户搜索--默认返回相关视频集合---匹配文案，标签
    @GetMapping("/search/video/{keyword}")
    public ResponseResult searchVideo(@PathVariable String keyword) throws IOException {
        System.out.println(keyword);
       List<Video> videos = searchService.searchVideByEs(keyword);
        if (Objects.isNull(videos)) {
            return new ResponseResult(409,"暂时没有相关记录");
        }
        return new ResponseResult(200,"搜索成功",videos);
    }

//    根据关键字搜索用户---匹配昵称，账号
    @GetMapping("/search/user/{keyword}")
    public ResponseResult searchUser(@PathVariable String keyword) throws IOException {
        List<User> users = searchService.searchUsersByEs(keyword);
        if (Objects.isNull(users)) {
            return new ResponseResult(409,"查找不到该用户");
        }
        return new ResponseResult(200,"搜索成功",users);
    }


    //获取搜索热词
    @GetMapping("/search/get/hot")
    public ResponseResult searchGetHot() throws IOException {
        return searchService.getTopNHotTerms();
    }






}
